﻿using DevExpress.XtraEditors;
using Lamex.Common;
using Lamex.Library;
using Lamex.Library.CommonClass;
using Lamex.mTools.en_us;
using Lamex.mTools.zh_Hans;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Lamex.mTools.UI
{
    public partial class frmTL_MergerExecelFile : frmBaseChild
    {
        DataTable dt = new DataTable();
        DataSet ds = null;
        ResourceManager rm = null;
        FileInfo[] filePathList = new FileInfo[] { };
        string FilePaths = "";
        Lamex.Common.IniFile ini = new Common.IniFile(Application.StartupPath + "\\config\\SavePath.ini");
        public frmTL_MergerExecelFile()
        {
            InitializeComponent();
            
            txtMergerFilePath.Text=  ini.IniReadValue("Path Configuration", "MergerPath").ToString();

            this.TextChanged += new EventHandler(frmTL_MergerExecelFile_TextChanged);
            btnExportExecel.Click += new EventHandler(btnExportExecel_Click);
            dt.Columns.Add("FileName",typeof(String));
            dt.Columns.Add("FilePath", typeof(String));
            dt.Columns.Add("FileColunmNum",typeof(int));
            dt.Columns.Add("Num", typeof(int));
            dt.Columns.Add("IsMergerFile",typeof(System.Boolean));
            ChangeLanguage();

            //string s1 = Library.CommonClass.IELogicHelper.GetLogicValue("A6FA087C", "3033227974", "?(W+D)*2");
            //string s2 = Library.CommonClass.IELogicHelper.GetLogicValue("A6FA087C", "3033227974", "?ROUNDDOWN(1530/H)");
            //string s3 = Library.CommonClass.IELogicHelper.GetLogicValue("A6FA087C", "3033227974", "?L:500,10|800,15|1800,25|,75");
            //string s4 = Library.CommonClass.IELogicHelper.GetLogicValue("A6FA087C", "3033227974", "?L:1000,(W:300,130|,220)|,(W:300,250|,580)");

            //string s5 = Library.CommonClass.IELogicHelper.GetLogicValue("A6FA087C", "3033227974", "?(L+W1)/2:500,(W:300,130|,220)|1000,(W:300,250|,580)|1500,((W+W1)/3:300,((D1+D2)/2:1000,700|,645)@(W:1400,300|,268))|,(W:300,500|,790)");
        }
        private void frmTL_MergerExecelFile_TextChanged(object sender,EventArgs e)
        {

            ChangeLanguage();
        }
        private void ChangeLanguage()
        {
            if (Loginer.CurrentUser.ChangeLanguage == 0)
            {
                rm = new ResourceManager(ResourceCNmTool.ResourceManager.BaseName, Assembly.GetExecutingAssembly());

            }
            else
            {
                rm = new ResourceManager(ResourceENmTool.ResourceManager.BaseName, Assembly.GetExecutingAssembly());
            }

            this.Text = rm.GetString("frmTL_MergerExecelFile");
            btnExportExecel.Text = rm.GetString("btnExportExecel");
            simpleButton1.Text = rm.GetString("simpleButton1");
            txtMergerFilePath.Caption = rm.GetString("txtMergerFilePath");
           

            LanguageObject.RegisterLanguage(gridView1);
          
        }
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            try
            {
                
                OpenFileDialog pfile = new OpenFileDialog();
                System.Windows.Forms.FolderBrowserDialog folder =new System.Windows.Forms.FolderBrowserDialog();
                if (!string.IsNullOrEmpty(txtMergerFilePath.Text)) folder.SelectedPath = txtMergerFilePath.Text;
                //pfile.ShowDialog();
                folder.ShowDialog();
                if (string.IsNullOrEmpty(folder.SelectedPath)) return;

                ds = new DataSet();
                dt.Clear();
                FileInfo files = new FileInfo(folder.SelectedPath);
                
                txtMergerFilePath.Text = folder.SelectedPath;
                filePathList = null;
                DirectoryInfo root = new DirectoryInfo(folder.SelectedPath);
                filePathList = root.GetFiles();
                FilePaths = txtMergerFilePath.Text;
                using (DevExpress.Utils.WaitDialogForm wdf = new DevExpress.Utils.WaitDialogForm("请稍等", "正在加载合并文件数据...", new Size(400, 100)))
                {
                    int m = 0;
                    int colNum = 0;
                    string strFirstName = "";
                    foreach (FileInfo info in filePathList)
                    {

                        if (!info.FullName.Contains("MergerFile"))
                        {
                            string ExcelName = "";
                            string strColName = "";
                            
                            int RowNum = 0;
                            DataRow dr = dt.NewRow();
                            int NumColumn= ExecelHelper.GetExcelColumnNum(0, true, info.FullName.ToString().ToLower(), ref ExcelName,ref RowNum,ref strColName);
                            dr["FileColunmNum"] = NumColumn;
                            dr["FileName"] = info.Name;
                            dr["FilePath"] = info.FullName+ @"\"+ExcelName;
                            dr["Num"] = RowNum;
                            if (m!=0)
                            {
                                if(colNum!= NumColumn)
                                dr["IsMergerFile"] = 0;
                                else
                                {
                                    if(strFirstName== strColName)
                                        dr["IsMergerFile"] = 1;
                                    else
                                        dr["IsMergerFile"] = 0;
                                }
                                    
                            }
                            else
                                dr["IsMergerFile"] = 1;


                            dt.Rows.Add(dr);
                            if (m == 0)
                            {
                                strFirstName = strColName;
                                colNum = NumColumn;
                            }
                            m++;
                        }

                    }
                    
                }
                //gridView1.OptionsView.ColumnAutoWidth = true ;
                gridControl1.DataSource = null;
                gridControl1.DataSource = dt;
                //gridView1.BestFitColumns();
                gridView1.OptionsView.ColumnAutoWidth = false;
                this.gridView1.BestFitColumns();
                ini.IniWriteValue("Path Configuration", "MergerPath", txtMergerFilePath.Text);
                //for (int I = 0; I < gridView1.Columns.Count; I++)
                //{

                //    this.gridView1.Columns[I].BestFit();//自动列宽
                //}

            }
            catch (Exception ex)
            { XtraMessageBox.Show(ex.Message); }
        }

        private void btnExportExecel_Click(object sender, EventArgs e)
        {
            
            try
            {
                using (DevExpress.Utils.WaitDialogForm wdf = new DevExpress.Utils.WaitDialogForm("请稍等", "正在合并数据...", new Size(400, 100)))
                {
                    ds.Clear();
                    ds.Tables.Clear();
                    for (int i = 0; i < gridView1.RowCount; i++)
                    {
                        string IsBool = gridView1.GetRowCellValue(i, "IsMergerFile").ToString();
                        if (IsBool.ToLower() == "true")
                        {
                            string fileName = gridView1.GetRowCellValue(i, "FilePath").ToString();
                            fileName = fileName.Substring(0, fileName.LastIndexOf(@"\"));
                            DataTable dts = ExecelHelper.ExcelConvertDataTable(0, true, fileName.ToString().ToLower());
                            dts.TableName = gridView1.GetRowCellValue(i, "FileName").ToString();
                            dts.Columns.Add("合并文件名称").SetOrdinal(0);
                            if (dts.Rows.Count != 0)
                                dts.Rows[0]["合并文件名称"] = gridView1.GetRowCellValue(i, "FileName").ToString();
                            if (dts.Rows.Count != 0)
                                ds.Tables.Add(dts);
                        }

                    }
                    if (ds.Tables.Count == 0) return;
                    
                    DataTable dtCopy = new DataTable();
                    dtCopy = ds.Tables[0].Clone();
                    foreach (DataTable dt in ds.Tables)
                    {
                            dtCopy.Merge(dt);
                        
                    }

                    ExecelHelper.DataTableToExcel(dtCopy, FilePaths + @"\MergerFile.xlsx");
                    
                }

                if (DialogResult.OK == XtraMessageBox.Show("合成文件存放在路径：" + FilePaths + @"\MergerFile.xlsx" + " 是否要打开此文件", "Confirm Message", MessageBoxButtons.OKCancel))
                {
                    System.Diagnostics.Process.Start(FilePaths + @"\MergerFile.xlsx");
                }
            }
            catch(Exception ex)
            {
                XtraMessageBox.Show(ex.Message);

            }
        }
    }
}
